<?php

class CloudSMS {

    public  $accesskey='wwmjbTNTmXvdhVhE';
    public  $secret='YbOKlgOCPvvjUcztPZgDaH8RHuaMaGJn';



    //添加geotable数据表 POST请求
    public  function sendMessages($mobile, $code) {
        $info = array();
        $url='https://api.1cloudsp.com/api/v2/single_send';

        $info['accesskey'] =$this->accesskey;
        $info['secret'] =$this->secret;
        $info['sign'] ='241656';
        $info['templateId'] ='205052';
        $info['mobile'] =$mobile;
        $info['content'] =urlencode($code);
        $result =$this->http_post($url, $info, false, '');

        return $result;
         
    }



    /**
     * GET 请求
     * @param string $url
     */
    private function http_get($url, $param) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        }
        $strGET = '';
        if (is_string($param)) {
            $strGET = $param;
        } else {
            $aGET = array();
            foreach ($param as $key => $val) {
                $aGET[] = $key . "=" . urlencode($val);
            }
            $strGET = join("&", $aGET);
        }
        $url = empty($strGET)?'':$url . '?' . $strGET;

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));

        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);

        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $post_file = false, $contenttype='') {

        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (empty($contenttype)) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
        }else if ($contenttype=="json") {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

        }

        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }


        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        $log = new Logging();
        $log->lfile('clound_sms');
        $log->lwrite(($url));
        $log->lwrite((json_encode($param)));
        $log->lwrite(($strPOST));
        $log->lwrite(($sContent));


        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }


    
}

?>
